﻿namespace Sidvall.AuthorizationManager.UI.Entities
{
    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Maintainability", "CA1506:AvoidExcessiveClassCoupling")]
    public sealed partial class AuthorizationContext : global::Sidvall.AuthorizationManager.UI.Entities.Core.BusinessBase, global::Sidvall.AuthorizationManager.Data.IAuthorizationContext
    {
        #region Business Members

        #region Action

        private string _Action = string.Empty;
        partial void OnBeforeActionPropertyGet(Sidvall.Business.PropertyEventData<string> eventData);
        partial void OnBeforeActionPropertyChanged(Sidvall.Business.PropertyChangeEventData<string> eventData);
        partial void OnAfterActionPropertyChanged();
        public string Action
        {
            get
            {
                Sidvall.Business.PropertyEventData<string> eventData = new Sidvall.Business.PropertyEventData<string>(_Action);
                OnBeforeActionPropertyGet(eventData);
                return eventData.Value;
            }
            set
            {
                Sidvall.Business.PropertyChangeEventData<string> eventData = new Sidvall.Business.PropertyChangeEventData<string>(value, _Action);
                OnBeforeActionPropertyChanged(eventData);
                if (!eventData.Cancel)
                {
                    LoadInstanceField("Action", eventData.Value, true, true, true);
                    OnAfterActionPropertyChanged();
                }
            }
        }

        #endregion
        #region AuthorizationContextId

        private System.Nullable<int> _AuthorizationContextId = null;
        partial void OnBeforeAuthorizationContextIdPropertyGet(Sidvall.Business.PropertyEventData<System.Nullable<int>> eventData);
        partial void OnBeforeAuthorizationContextIdPropertyChanged(Sidvall.Business.PropertyChangeEventData<System.Nullable<int>> eventData);
        partial void OnAfterAuthorizationContextIdPropertyChanged();
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
        public System.Nullable<int> AuthorizationContextId
        {
            get
            {
                Sidvall.Business.PropertyEventData<System.Nullable<int>> eventData = new Sidvall.Business.PropertyEventData<System.Nullable<int>>(_AuthorizationContextId);
                OnBeforeAuthorizationContextIdPropertyGet(eventData);
                return eventData.Value;
            }
            internal set
            {
                Sidvall.Business.PropertyChangeEventData<System.Nullable<int>> eventData = new Sidvall.Business.PropertyChangeEventData<System.Nullable<int>>(value, _AuthorizationContextId);
                OnBeforeAuthorizationContextIdPropertyChanged(eventData);
                if (!eventData.Cancel)
                {
                    LoadInstanceField("AuthorizationContextId", eventData.Value, true, true, true);
                    OnAfterAuthorizationContextIdPropertyChanged();
                }
            }
        }

        #endregion
        #region ClaimType

        private string _ClaimType = string.Empty;
        partial void OnBeforeClaimTypePropertyGet(Sidvall.Business.PropertyEventData<string> eventData);
        partial void OnBeforeClaimTypePropertyChanged(Sidvall.Business.PropertyChangeEventData<string> eventData);
        partial void OnAfterClaimTypePropertyChanged();
        public string ClaimType
        {
            get
            {
                Sidvall.Business.PropertyEventData<string> eventData = new Sidvall.Business.PropertyEventData<string>(_ClaimType);
                OnBeforeClaimTypePropertyGet(eventData);
                return eventData.Value;
            }
            set
            {
                Sidvall.Business.PropertyChangeEventData<string> eventData = new Sidvall.Business.PropertyChangeEventData<string>(value, _ClaimType);
                OnBeforeClaimTypePropertyChanged(eventData);
                if (!eventData.Cancel)
                {
                    LoadInstanceField("ClaimType", eventData.Value, true, true, true);
                    OnAfterClaimTypePropertyChanged();
                }
            }
        }

        #endregion
        #region ClaimValue

        private string _ClaimValue = string.Empty;
        partial void OnBeforeClaimValuePropertyGet(Sidvall.Business.PropertyEventData<string> eventData);
        partial void OnBeforeClaimValuePropertyChanged(Sidvall.Business.PropertyChangeEventData<string> eventData);
        partial void OnAfterClaimValuePropertyChanged();
        public string ClaimValue
        {
            get
            {
                Sidvall.Business.PropertyEventData<string> eventData = new Sidvall.Business.PropertyEventData<string>(_ClaimValue);
                OnBeforeClaimValuePropertyGet(eventData);
                return eventData.Value;
            }
            set
            {
                Sidvall.Business.PropertyChangeEventData<string> eventData = new Sidvall.Business.PropertyChangeEventData<string>(value, _ClaimValue);
                OnBeforeClaimValuePropertyChanged(eventData);
                if (!eventData.Cancel)
                {
                    LoadInstanceField("ClaimValue", eventData.Value, true, true, true);
                    OnAfterClaimValuePropertyChanged();
                }
            }
        }

        #endregion
        #region Resource

        private string _Resource = string.Empty;
        partial void OnBeforeResourcePropertyGet(Sidvall.Business.PropertyEventData<string> eventData);
        partial void OnBeforeResourcePropertyChanged(Sidvall.Business.PropertyChangeEventData<string> eventData);
        partial void OnAfterResourcePropertyChanged();
        public string Resource
        {
            get
            {
                Sidvall.Business.PropertyEventData<string> eventData = new Sidvall.Business.PropertyEventData<string>(_Resource);
                OnBeforeResourcePropertyGet(eventData);
                return eventData.Value;
            }
            set
            {
                Sidvall.Business.PropertyChangeEventData<string> eventData = new Sidvall.Business.PropertyChangeEventData<string>(value, _Resource);
                OnBeforeResourcePropertyChanged(eventData);
                if (!eventData.Cancel)
                {
                    LoadInstanceField("Resource", eventData.Value, true, true, true);
                    OnAfterResourcePropertyChanged();
                }
            }
        }

        #endregion
        #region ResourceItemId

        private string _ResourceItemId = string.Empty;
        partial void OnBeforeResourceItemIdPropertyGet(Sidvall.Business.PropertyEventData<string> eventData);
        partial void OnBeforeResourceItemIdPropertyChanged(Sidvall.Business.PropertyChangeEventData<string> eventData);
        partial void OnAfterResourceItemIdPropertyChanged();
        public string ResourceItemId
        {
            get
            {
                Sidvall.Business.PropertyEventData<string> eventData = new Sidvall.Business.PropertyEventData<string>(_ResourceItemId);
                OnBeforeResourceItemIdPropertyGet(eventData);
                return eventData.Value;
            }
            set
            {
                Sidvall.Business.PropertyChangeEventData<string> eventData = new Sidvall.Business.PropertyChangeEventData<string>(value, _ResourceItemId);
                OnBeforeResourceItemIdPropertyChanged(eventData);
                if (!eventData.Cancel)
                {
                    LoadInstanceField("ResourceItemId", eventData.Value, true, true, true);
                    OnAfterResourceItemIdPropertyChanged();
                }
            }
        }

        #endregion
        #region SubAction

        private string _SubAction = string.Empty;
        partial void OnBeforeSubActionPropertyGet(Sidvall.Business.PropertyEventData<string> eventData);
        partial void OnBeforeSubActionPropertyChanged(Sidvall.Business.PropertyChangeEventData<string> eventData);
        partial void OnAfterSubActionPropertyChanged();
        public string SubAction
        {
            get
            {
                Sidvall.Business.PropertyEventData<string> eventData = new Sidvall.Business.PropertyEventData<string>(_SubAction);
                OnBeforeSubActionPropertyGet(eventData);
                return eventData.Value;
            }
            set
            {
                Sidvall.Business.PropertyChangeEventData<string> eventData = new Sidvall.Business.PropertyChangeEventData<string>(value, _SubAction);
                OnBeforeSubActionPropertyChanged(eventData);
                if (!eventData.Cancel)
                {
                    LoadInstanceField("SubAction", eventData.Value, true, true, true);
                    OnAfterSubActionPropertyChanged();
                }
            }
        }

        #endregion
        #region UserName

        private string _UserName = string.Empty;
        partial void OnBeforeUserNamePropertyGet(Sidvall.Business.PropertyEventData<string> eventData);
        partial void OnBeforeUserNamePropertyChanged(Sidvall.Business.PropertyChangeEventData<string> eventData);
        partial void OnAfterUserNamePropertyChanged();
        public string UserName
        {
            get
            {
                Sidvall.Business.PropertyEventData<string> eventData = new Sidvall.Business.PropertyEventData<string>(_UserName);
                OnBeforeUserNamePropertyGet(eventData);
                return eventData.Value;
            }
            set
            {
                Sidvall.Business.PropertyChangeEventData<string> eventData = new Sidvall.Business.PropertyChangeEventData<string>(value, _UserName);
                OnBeforeUserNamePropertyChanged(eventData);
                if (!eventData.Cancel)
                {
                    LoadInstanceField("UserName", eventData.Value, true, true, true);
                    OnAfterUserNamePropertyChanged();
                }
            }
        }

        #endregion
        #region DataRowContextStorageKey

        protected override string DataRowContextStorageKey
        {
            get
            {
                return global::Sidvall.AuthorizationManager.UI.SystemContextConfig.Current.EntityFacade.AuthorizationContextManager.GetKey(this);
            }
        }

        #endregion
        #region LoadInstanceField

        public void LoadInstanceField(string fieldName, object value, bool cascade, bool raisePropertyChanged, bool updateState)
        {
            global::Sidvall.AuthorizationManager.UI.SystemContextConfig.Current.EntityFacade.AuthorizationContextManager.LoadInstanceField(this, fieldName, value, cascade, raisePropertyChanged, updateState);
        }

        #endregion
        #region AcceptChanges

        public void AcceptChanges()
        {
            global::Sidvall.AuthorizationManager.UI.SystemContextConfig.Current.EntityFacade.AuthorizationContextManager.AcceptChanges(this);
        }

        #endregion
        #region Clone

        public AuthorizationContext Clone()
        {
            var copySettings = Sidvall.Data.CopySettings.Clone();
            return Copy(copySettings);
        }

        #endregion
        #region Copy

        public AuthorizationContext Copy()
        {
            return Copy(null);
        }
        public AuthorizationContext Copy(Sidvall.Data.CopySettings copySettings)
        {
            if (copySettings == null)
                copySettings = new Sidvall.Data.CopySettings();
            if (!Sidvall.Data.DataRowContextManager.IsValid(this, copySettings.RowFilter))
                return null;
            var item = new AuthorizationContext();
            Import(item, this, copySettings);
            return item;
        }

        #endregion
        #region Import

        public void Import(global::Sidvall.AuthorizationManager.Data.IAuthorizationContext item)
        {
            Import(item, null);
        }
        public void Import(global::Sidvall.AuthorizationManager.Data.IAuthorizationContext item, Sidvall.Data.CopySettings copySettings)
        {
            Import(this, item, copySettings);
        }
        partial void OnAfterImport(AuthorizationContext destination, global::Sidvall.AuthorizationManager.Data.IAuthorizationContext source, Sidvall.Data.CopySettings copySettings);
        private void Import(AuthorizationContext destination, global::Sidvall.AuthorizationManager.Data.IAuthorizationContext source, Sidvall.Data.CopySettings copySettings)
        {
            global::Sidvall.AuthorizationManager.UI.SystemContextConfig.Current.EntityFacade.AuthorizationContextManager.Import(destination, source, copySettings);
            OnAfterImport(destination, source, copySettings);
        }
        partial void OnAfterImport(AuthorizationContext destination, AuthorizationContext source, Sidvall.Data.CopySettings copySettings);
        private void Import(AuthorizationContext destination, AuthorizationContext source, Sidvall.Data.CopySettings copySettings)
        {
            Import(destination, source as global::Sidvall.AuthorizationManager.Data.IAuthorizationContext, copySettings);
            OnAfterImport(destination, source, copySettings);
        }

        #endregion
        #region ValidateAsync

        public async System.Threading.Tasks.Task<Sidvall.Data.BusinessRuleCollection> ValidateAsync()
        {
            return await ValidateAsync((Sidvall.Data.ValidationSettings)null);
        }
        public async System.Threading.Tasks.Task<Sidvall.Data.BusinessRuleCollection> ValidateAsync(Sidvall.Data.ValidationSettings settings)
        {
            var context = new Sidvall.Business.ValidationContext(settings);
            await global::Sidvall.AuthorizationManager.UI.SystemContextConfig.Current.EntityFacade.AuthorizationContextManager.ValidateAsync(this, context);
            await ValidateAsync(context);
            return context.AllBusinessRules;
        }
        partial void OnAfterValidate(Sidvall.Data.BusinessRuleCollection itemBusinessRules, Sidvall.Business.ValidationContext context);
        public async System.Threading.Tasks.Task ValidateAsync(Sidvall.Business.ValidationContext context)
        {
            var itemBusinessRules = new Sidvall.Data.BusinessRuleCollection();
            OnAfterValidate(itemBusinessRules, context);
            await context.TaskContext.DoTaskAsync();
            context.AllBusinessRules.AddRange(context.Settings.IncludeNotBrokenRules, itemBusinessRules);
            var businessRulesProvider = this as Sidvall.Data.IBusinessRulesProvider;
            if (businessRulesProvider != null)
                businessRulesProvider.BusinessRules.AddRange(context.Settings.IncludeNotBrokenRules, itemBusinessRules);
        }

        #endregion
        #region Initialize

        public void Initialize()
        {
            global::Sidvall.AuthorizationManager.UI.SystemContextConfig.Current.EntityFacade.AuthorizationContextManager.Initialize(this);
        }
        public void Initialize(Sidvall.Data.InitializeSettings settings)
        {
            global::Sidvall.AuthorizationManager.UI.SystemContextConfig.Current.EntityFacade.AuthorizationContextManager.Initialize(this, settings);
        }

        #endregion
        
        #region IAuthorizationContext Members

        string global::Sidvall.AuthorizationManager.Data.IAuthorizationContext.Action
        {
            get
            {
                return this._Action;
            }
            set
            {
                this._Action = value;
            }
        }
        System.Nullable<int> global::Sidvall.AuthorizationManager.Data.IAuthorizationContext.AuthorizationContextId
        {
            get
            {
                return this._AuthorizationContextId;
            }
            set
            {
                this._AuthorizationContextId = value;
            }
        }
        string global::Sidvall.AuthorizationManager.Data.IAuthorizationContext.ClaimType
        {
            get
            {
                return this._ClaimType;
            }
            set
            {
                this._ClaimType = value;
            }
        }
        string global::Sidvall.AuthorizationManager.Data.IAuthorizationContext.ClaimValue
        {
            get
            {
                return this._ClaimValue;
            }
            set
            {
                this._ClaimValue = value;
            }
        }
        string global::Sidvall.AuthorizationManager.Data.IAuthorizationContext.Resource
        {
            get
            {
                return this._Resource;
            }
            set
            {
                this._Resource = value;
            }
        }
        string global::Sidvall.AuthorizationManager.Data.IAuthorizationContext.ResourceItemId
        {
            get
            {
                return this._ResourceItemId;
            }
            set
            {
                this._ResourceItemId = value;
            }
        }
        string global::Sidvall.AuthorizationManager.Data.IAuthorizationContext.SubAction
        {
            get
            {
                return this._SubAction;
            }
            set
            {
                this._SubAction = value;
            }
        }
        string global::Sidvall.AuthorizationManager.Data.IAuthorizationContext.UserName
        {
            get
            {
                return this._UserName;
            }
            set
            {
                this._UserName = value;
            }
        }

        #endregion

        #endregion
        #region Constructors

        public AuthorizationContext()
        {
        }

        public AuthorizationContext(Sidvall.Data.DataRowState? dataRowState)
            : base(dataRowState)
        {
        }

        #endregion
    }
}